package com.software.date11_04;

import java.io.*;

public class Test {
    /**
     * 今天讲了IO流相关操作和用法
     * @param args
     */
    public static void main(String[] args) {
        BufferedReader br = null;
        try{
            FileReader fr = new FileReader("D:/hello.txt");
            //()里面使用对应的字符流或字节流
            br = new BufferedReader(fr);
            //打印D:/hello.txt文件的每一行，一行一行打印
            String s = null;
            while((s = br.readLine()) != null){
                System.out.println(s);
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
        if(br != null){
            try{
                br.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
    }
    public static void main5(String[] args) {
        FileWriter fw = null;
        try{
            fw = new FileWriter("D:/hello.txt");
            fw.write("今晚我要看永夜星河！！！");
            fw.flush();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(fw != null){
                try{
                    fw.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main4(String[] args) {
        FileReader fr = null;
        try{
            fr = new FileReader("D:/hello.txt");
            char[] ch = new char[4];
            int read = 0;
            while((read = fr.read(ch)) != -1){
                String s = new String(ch,0,read);
                System.out.print(s);
            }

        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(fr != null){
                try{
                    fr.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main3(String[] args) {
        FileOutputStream fos = null;
        try{//默认情况下会覆盖
            fos = new FileOutputStream("D:/hello.txt");
            //这样写后续.write()不会覆盖，是append追加
            // fos = new FileOutputStream("D:/hello.txt",true);
            fos.write("hello java".getBytes());
            //刷新，一定要加
            fos.flush();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(fos != null) {
                try{
                    fos.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }

        }
    }
    public static void main2(String[] args) {
        FileInputStream fis = null;
        try{
            fis = new FileInputStream("D:/hello.txt");
            int read = 0;
            byte[] bytes = new byte[4];//fis.read()的参数可以是byte[]类型的数组
            while((read = fis.read(bytes)) != -1){
                //System.out.println(read);这里read代表读到的个数
                String str = new String(bytes,0,read);
                System.out.println(str);
            }
        }catch(FileNotFoundException e){

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try{
                fis.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
    public static void main1(String[] args) {
        FileInputStream fis = null;
        try{
            fis = new FileInputStream("D:/hello.txt");
//            while(true){
//                int read = fis.read();
//                if(read == -1) {
//                    break;
//                }
//                System.out.print(read);
//            }
            //读到的read是ASCII码值
            int read = 0;
            while((read = fis.read()) != -1){
                System.out.println(read);
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                fis.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}
